from enum import Enum
from typing import Optional
from typing import Union

from pydantic import BaseModel, Field


class ProcedureEnum(Enum):
    none = 0
    session_abnormal = 1
    session_abnormal_get_dangerous = 2
    session_normal = 3
    pcap = 4
    label_abnormal = 5
    label_normal = 6
    mapping_object = 7
    mapping_not_object = 8
    replace_fields = 9


class ApiQueryBean(BaseModel):
    startTime: str = Field("", description="数据开始时间")
    endTime: str = Field("", description="数据结束时间")
    expression: str = Field("", description="arkime的表达式")
    splitNumber: int = Field(5000, description="每隔splitNumber分成一个文件")
    catalogue: str = Field("/app/", description="数据提取的目录")
    requestCode: int = Field(description="请求ID")
    callBackUrl: str = Field(description="请求成功后的回掉地址")


class ApiCurrentConfigBean(BaseModel):
    id: Optional[int] = Field(None, description="配置文件ID")
    description: Optional[str] = Field(None, description="配置文件描述")


class ApiConfigBean(BaseModel):
    id: Optional[int] = Field(None, description="配置文件的ID")

    label_all_true: Optional[bool] = Field(False, description="打标签，默认将isDangerous赋值为label_all_true")
    label_is_output_unmatch: Optional[bool] = Field(False,
                                                    description="打标签，是否输出没有和安全设备的异常数据匹配成功的数据文件，若输出，请在config.yaml配置路径")
    session_all_true: Optional[bool] = Field(False,
                                             description="是否先获取session为异常的数据，即先读取所有异常数据，在根据异常数据获取session")

    mapping_le_path: Optional[str] = Field(None, description="对数据做映射的向量文件路径，目前已废弃")
    replace_source: Optional[str] = Field(None, description="替换固定列的源文件，用逗号分割路径")
    replace_destination: Optional[str] = Field(None, description="被替换文件")
    replace_mapping: Optional[str] = Field(None,
                                           description="用作替换的列名映射，用分号分割，格式： 源码文件字段A:，目标文件字段A;源码文件字段B:目标文件字段B;...")
    replace_save_to: Optional[str] = Field(None, description="文件替换的保存路径")
    session_start_time: Optional[str] = Field(None,
                                              description="获取数据的开始时间，格式：%Y-%m-%d %H:%M:%S或%Y/%m/%d %H:%M:%S")
    session_end_time: Optional[str] = Field(None,
                                            description="获取数据的结束时间，格式：%Y-%m-%d %H:%M:%S或%Y/%m/%d %H:%M:%S")
    session_expression: Optional[str] = Field(None, description="获取数据的arkime表达式")
    session_alive: Optional[str] = Field(None, description="获取数据的滚动ID存活时间，参考elasticsearch的scroll")
    catalogue: Optional[str] = Field(None, description="数据提取的目录")
    description: Optional[str] = Field(None, description="配置文件的描述")

    pcap_per_subsection: Optional[int] = Field(None, description="每批次获取pcap文件的数量")
    pcap_process: Optional[int] = Field(None, description="获取pcap数据的进程数量")
    pcap_thread_in_process: Optional[int] = Field(None, description="获取pcap数据进程的线程数量")
    session_size: Optional[int] = Field(None, description="elasticsearch每次获取session数据的大小")
    label_duration: Optional[int] = Field(None, description="打标签时容许的误差偏移时间（分钟）")
    splitNumber: Optional[int] = Field(None, description="每隔splitNumber分成一个文件")

    # other
    type_: Optional[str] = None


class ApiFlowBean(BaseModel):
    id: Optional[int] = Field(None, description="获取数据流程的id")
    step: Optional[str] = Field(None, description="获取数据的流程步骤逗号分割，可选session,pcap,label,replace")
    description: Optional[str] = Field(None, description="流程描述")
    flow_id: Optional[Union[int, str]] = Field(None, description="流程ID")


class ApiCommonBean(BaseModel):
    type: Optional[Union[int, str]] = Field(None, description="通用数据类型，类型")
    id: Optional[Union[int, str]] = Field(None, description="通用数据类型，id")
    filename: Optional[Union[int, str]] = Field(None, description="通用数据类型，文件名")
